package cn.biplam.front.component.view.systemManage.datasource;

import cn.biplam.common.client.DataFilter;
import cn.biplam.common.client.Operator;
import cn.biplam.common.client.OperatorResult;
import cn.biplam.common.opcode.mdManage.TypeCode.DbSourceCode;
import cn.biplam.common.struc.MdStruc;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.UserInfo;
import cn.biplam.front.component.AjxResponse;
import cn.biplam.front.component.view.common.DatabaseSelect;
import cn.biplam.front.component.view.mdManage.DsTypeSelect;
import cn.biplam.front.component.view.mdManage.ModelTypeSelect;
import cn.biplam.front.html.HtmlDiv;
import cn.biplam.front.html.HtmlText;
import cn.biplam.front.html.base.HtmlWindow;
import cn.biplam.front.html.base.Const.Length_Unit;
import cn.biplam.front.htmlscript.script.AjxScript;
import cn.biplam.front.htmlscript.script.ScriptCustomer;
import cn.biplam.front.htmlscript.script.ScriptPrompt.Close_Prompt;
import cn.biplam.front.template.base.SimpleInput;
import cn.biplam.front.template.base.SimpleTextArea;
import cn.biplam.front.template.control.FlatTable;

/**
 * 元数据管理 — 数据源管理
 * @author XiaoRuxing 2009-6-30下午03:24:21
 * @version 1.0
 */
public class DataSourceAdd extends AjxResponse{
	
	/** 组件ID */
	public static String cId="8202";
	/** 获取添加数据源界面动作ID */
	public static String Add_DbSource="820201";
	/** 添加数据源操作动作ID */
	public static String Add_DbSource_Opt="820202";
	private String width = "420";
	
	@Override
	protected HtmlWindow ajxGet() throws ErrorMessage {
		if(actionId.equals(Add_DbSource)){
			return _getAddPanel();
		}else if(actionId.equals(Add_DbSource_Opt)){
			return _addOpt();
		}else{
			throw new ErrorMessage("Ajx Exchange outport not found:"+ actionId+" !");
		}
	}
	
	/**
	 * 获取添加数据源界面
	 * @return
	 * @throws ErrorMessage
	 */
	private HtmlWindow _getAddPanel() throws ErrorMessage{
		HtmlWindow outer = new HtmlDiv();
		HtmlWindow panel=_drawPanel(mUser,null,sHomeUrl);
//		ScriptCustomer.getDbsSubmitCheckInput(panel,cId, Add_DbSource_Opt,containerId,Is_RefreshParent.NoRefresh,
//				Is_AddParam.NoAdd,Close_Prompt.Close);
		ScriptCustomer.getDbsSubmitCheckInput(panel,cId, Add_DbSource_Opt,Close_Prompt.Close);
		panel.setStyleWidth(width);
		outer.put(panel);
		return panel;
	}
	
	/**
	 * 数据源信息添加或编辑界面 
	 * @throws ErrorMessage
	 */
	private HtmlWindow _drawPanel(UserInfo mUser,OperatorResult sr,String sHomeUrl) throws ErrorMessage{
		String dbType=null;
		String dssid=null;
		String ip = null;
		String port = null;
		String mainDb=null;
		String dbUser=null;
		String dbPassword=null;
		String dbName = null;
		String dsType =null;
		if(sr!=null&&!sr.isNull()){
			dbType = sr.getValue(MdStruc.DbSourStruc.DB_TYPE_ID);
			dssid = sr.getValue(MdStruc.DbSourStruc.DB_SOURCE_SID);
			dbUser = sr.getValue(MdStruc.DbSourStruc.USRE_NAME);
			dbPassword = sr.getValue(MdStruc.DbSourStruc.PASSWORD);
			dbName = sr.getValue(MdStruc.DbSourStruc.DB_SOURCE_NAME);
			mainDb = sr.getValue(MdStruc.DbSourStruc.MAIN_DB);
			ip = sr.getValue(MdStruc.DbSourStruc.IP);
			port = sr.getValue(MdStruc.DbSourStruc.PORT);
			dsType = sr.getValue(MdStruc.DbSourStruc.DSTYPE);
			
		}
		
		FlatTable table = new FlatTable(cId,recordId);
		
		table.putTh("数据源名称",0,0);
		table.putTd(new SimpleInput(MdStruc.DbSourStruc.DB_SOURCE_NAME,"52",dbName).get(), 0, 1,"3");
		table.putTh("数据源说明",1,0);
		table.putTd(new SimpleTextArea(MdStruc.DbSourStruc.DB_SOURCE_DESC,"50","").get(),1,1,"3");
		table.putTh("数据库类型",2,0);
		table.putTd(DatabaseSelect.get(MdStruc.DbSourStruc.DB_TYPE_ID, dbType, mUser, sHomeUrl,"setDbsInput(this)"), 2, 1);
		table.putTh("模型类型",2,2);
		table.putTd(ModelTypeSelect.get(MdStruc.DbSourStruc.MODEL_TYPE, dbType, mUser, sHomeUrl), 2, 3);
		table.putTh("主机/IP", 3, 0);
		table.putTd(new SimpleInput(MdStruc.DbSourStruc.IP,"17",ip).get(), 3, 1);
		table.putTh("端口", 3, 2);
		table.putTd(new SimpleInput(MdStruc.DbSourStruc.PORT,"17",port).get(), 3, 3);
		table.putTh("数据库实例名", 4, 0);
		table.putTd(new SimpleInput(MdStruc.DbSourStruc.DB_SOURCE_SID,"17",dssid).get(), 4, 1);
		table.putTh("数据库名", 4, 2);
		table.putTd(new SimpleInput(MdStruc.DbSourStruc.MAIN_DB,"17",mainDb).get(), 4, 3);
		table.putTh("用户",5,0);
		table.putTd(new SimpleInput(MdStruc.DbSourStruc.USRE_NAME,"17",dbUser).get(), 5, 1);
		table.putTh("口令",5,2);
		table.putTd(new SimpleInput(MdStruc.DbSourStruc.PASSWORD,"17",dbPassword).get(), 5, 3);
		table.putTh("数据源类型",6,0);
		table.putTd(DsTypeSelect.get(MdStruc.DbSourStruc.DSTYPE, dsType, mUser.getUserID(), sHomeUrl), 6, 1);
		
		table.setColWidth(0,"20",Length_Unit.percent);
		table.setWidth(width);
		return table.get();
	}
	
	public HtmlWindow _addOpt() throws ErrorMessage{
		HtmlWindow win=new HtmlText();
		DataFilter filter=new DataFilter();
		filter.put(MdStruc.DbSourStruc.DB_TYPE_ID, params.getByKey(MdStruc.DbSourStruc.DB_TYPE_ID+"value"));
		filter.put(MdStruc.DbSourStruc.MODEL_TYPE, params.getByKey(MdStruc.DbSourStruc.MODEL_TYPE+"value"));
		filter.put(MdStruc.DbSourStruc.DSTYPE, params.getByKey(MdStruc.DbSourStruc.DSTYPE+"value"));
		filter.putAll(this.params);	
		Operator.get(DbSourceCode.Insert, mUser.getUserID(), filter);
		win.bind(AjxScript.getA(DataSourceMain.cId,DataSourceMain.aId_main,"refresh"));
		return win;
	}
}
